package com.kxf.androidtestdemo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.kxf.androidtestdemo.R;
import com.kxf.androidtestdemo.adapter.NormalAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NestedScrollViewTestActivity extends AppCompatActivity {

    private NestedScrollView nestedScrollView;
    private boolean isTop = true;
    private TextView tv1, tv2, tv3;
    private long lastChangeState = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nested_scroll_view_test);

        List<String> data = new ArrayList<>();
        for (int i=0; i<30;i++){
            data.add("item==" + i);
        }
        nestedScrollView = findViewById(R.id.nestedScrollView);
        RecyclerView rv = (RecyclerView) findViewById(R.id.recyclerView);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);

        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new NormalAdapter(data));
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.i("nestedScrollView", "scrollY=" + scrollY);
                Log.i("nestedScrollView", "oldScrollY=" + oldScrollY);
                if (false){

                }else {
                    if (isTop){
                        if (scrollY>dip2px(NestedScrollViewTestActivity.this, 50)){
                            Log.i("nestedScrollView", "开始向上滑动");
                            tv1.setVisibility(View.GONE);
                            isTop = false;
                            lastChangeState = System.currentTimeMillis();
                        }
                    }else {
                        if (scrollY<=dip2px(NestedScrollViewTestActivity.this, 50)){
                            Log.i("nestedScrollView", "到顶部了");
                            tv1.setVisibility(View.VISIBLE);
                            isTop = true;
                            lastChangeState = System.currentTimeMillis();
                        }
                    }
                }
            }
        });
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}